home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / tcoop.arc / TCOOP2.ARC / BUTTON1.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-26  |  2.8 KB  |  147 lines

  1. // button1.cpp: Button demo program
  2.  
  3. #include "string.h"
  4. #include "wsotxscr.h"
  5.  
  6. typedef void (*ActionProc)(Wso *Src, MsgPkt &M);
  7.  
  8. class Button : public Wso {
  9. public:
  10.   ActionProc Action;
  11.   char *Name;
  12.   Button(char *N, ColorPak &Cp, ActionProc A);
  13.   virtual ~Button(void);
  14.   virtual void Draw(void); 
  15.   virtual void Activate(MsgPkt &M); 
  16. };
  17.  
  18. class Count : public Wso {
  19. public:
  20.   int C;
  21.   Count(void);
  22.   virtual void OnMouseEnter(MsgPkt &M)  { SwitchFocus(M); }
  23.   virtual void OnMouseLeave(MsgPkt &M)  { Leave(M); }
  24.   virtual void OnMouseWithin(MsgPkt &M);
  25. };
  26.  
  27. // ---------------- Button methods ------------------- 
  28.  
  29. Button::Button(char *N, ColorPak &Cp, ActionProc A)
  30. : Wso(0x11, ButtonStyle, Cp)
  31. {
  32.   Name = strdup(N);
  33.   SetSize(strlen(Name)+2, 1);
  34.   Action = A;
  35. }
  36.  
  37. Button::~Button(void) { free(Name); }
  38.  
  39. void Button::Draw(void)
  40. {
  41.   Wso::Draw();
  42.   Panel->HzWrtB(1, 0, Name);
  43. }
  44.  
  45. void Button::Activate(MsgPkt &M)
  46. {
  47.   if (Active) {
  48.      Wso::Activate(M);
  49.      Action(this, M);
  50.   }
  51.   else {
  52.     Leave(M);
  53.   }
  54. }
  55.  
  56. // ------------ Some common button actions --------- 
  57.  
  58. void NoOp(Wso *, MsgPkt &) { ; }
  59.  
  60. void ExitAction(Wso *, MsgPkt &M)
  61. {
  62.   M.RtnCode = ShutDown;
  63. }
  64.  
  65. void Popup(Wso *Src, MsgPkt &M)
  66. {
  67.   Wso *B;
  68.   Src->Leave(M);
  69.   B = new Wso(0x11, WindowStyle, DefColors);
  70.   B->SetSize(30, 3);
  71.   B->Open(FullScrn, 30, 10);
  72.   B->Panel->HzWrt(1, 0, ((Button *)Src)->Name, 0);
  73.   B->SwitchFocus(M);
  74.   M.RtnCode = M.Code;
  75.   do {
  76.     B->SubMgr->EventStep(M);
  77.     M.Code = M.RtnCode;
  78.   } while (M.Code != ShutDown &&
  79.           (!B->Active || M.Code != Close || M.Focus != (Iso *)(B)));
  80.   if (M.Code == Close) {
  81.      B->OnClose(M);
  82.      Mouse.WaitForEvent(MouseUp, M.Mx, M.My);
  83.      M.RtnCode = Idle;
  84.   }
  85.   delete B;
  86. }
  87.  
  88. // ---------------- Count object methods ---------------
  89.  
  90. Count::Count(void) : Wso(0x11, WindowStyle, DefColors)
  91. {
  92.   SetSize(6, 1);
  93.   C = 0;
  94. }
  95.  
  96. void Count::OnMouseWithin(MsgPkt &M)
  97. {
  98.   char Buff[10];
  99.   if (Panel->OnInterior(M.Mx, M.My)) {
  100.      itoa(++C, Buff, 10);
  101.      Panel->HzWrt(0, 0, Buff, 0);
  102.   }
  103. }
  104.  
  105. Button *MsgBut;
  106. Button *ExitBut;
  107. Wso *S;
  108. Count *CountBut;
  109.  
  110. main()
  111. {
  112.   Setup(MouseOptional, MonoColors);
  113.   S = new Wso(0x11, WindowStyle, DefColors);
  114.   S->SetSize(60, 20);
  115.   S->Open(FullScrn, 0, 0);
  116.   S->Panel->Clear(177, 7);
  117.   // Create and display the exit and message buttons 
  118.   MsgBut = new Button("Message Button", DefColors, Popup);
  119.   // CORRECTION: Pg 204 of the manuscript says &DefColors
  120.   //             but ColorPak is passed by reference
  121.   ExitBut = new Button("Exit", DefColors, ExitAction);
  122.   MsgBut->Open(S, 3, 3);
  123.   ExitBut->Open(S, 6, 6);
  124.   // Create and display the counter button 
  125.   CountBut = new Count;
  126.   CountBut->Open(S, 22, 12);
  127.   MainEventLoop();
  128.   CleanUp();  // Deletes buttons too
  129. }
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.        
  147.